#!/bin/sh

# start/stop/restart/reload cfservd

# 'cfservd' looks for a configuration file cfservd.conf by default.

# Note: this daemon doesn't actually need to run under the root account,
# assuming an account named 'cfservd' exists, one way of configuring it
# to use its own account would be to to run 'cfkey' and 'cfagent' ones
# which creates the ~/.cfagent subdir then start the service with:
# /bin/su - cfservd -c /usr/sbin/cfservd

# Start cfservd:
cfservd_start() {
  if [ -x /usr/sbin/cfservd ]; then
    # Make sure localhost keys exist first
    if [ ! -f /var/cfengine/ppkeys/localhost.priv ]; then
      /usr/sbin/cfkey
    fi
    echo "Starting Cfengine:  /usr/sbin/cfservd"
    /usr/sbin/cfservd
  fi
}

# Stop cfservd:
cfservd_stop() {
  /bin/killall cfservd 2> /dev/null
}

# Restart cfservd:
cfservd_restart() {
  cfservd_stop
  sleep 1
  cfservd_start
}

# Reload cfservd:
cfservd_reload() {
  /bin/killall -HUP cfservd
}

case "$1" in
'start')
  cfservd_start
  ;;
'stop')
  cfservd_stop
  ;;
'restart')
  cfservd_restart
  ;;
'reload')
  cfservd_reload
  ;;
*)
  echo "usage $0 start|stop|restart|reload"
esac
